package com.zmn.oms.services.impl.acceptance;

import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.common.annotation.OrderWorkChangeSync;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.model.dto.acceptance.OrderWorkAcceptanceDTO;
import com.zmn.oms.model.entity.acceptance.OrderWorkAcceptance;
import com.zmn.oms.model.entity.acceptance.OrderWorkAcceptanceDbQuery;
import com.zmn.oms.persistence.interfaces.acceptance.OrderWorkAcceptanceDao;
import com.zmn.oms.services.interfaces.acceptance.OrderWorkAcceptanceService;
import io.shardingsphere.api.HintManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：验收业务实现
 *
 * @author wangxiaokun
 * @since 2021/09/18 14:21
 */
@Service
public class OrderWorkAcceptanceServiceImpl implements OrderWorkAcceptanceService {

    @Resource
    OrderWorkAcceptanceDao orderWorkAcceptanceDao;

    /**
     * 根据订单id集合查询验收列表
     *
     * @param orderIdList
     * @return
     */
    @Override
    public List<OrderWorkAcceptance> listOrderWorkAcceptanceByOrderIdList(List<Long> orderIdList) {
        return orderWorkAcceptanceDao.listOrderWorkAcceptanceByOrderIdList(orderIdList);
    }

    /**
     * 根据查询条件查询订单验收数量
     * @param query
     * @return
     */
    @Override
    public int countOrderWorkAcceptanceByQuery(OrderWorkAcceptanceDbQuery query) {
        return orderWorkAcceptanceDao.countOrderWorkAcceptanceByQuery(query);
    }

    /**
     * 查询单个订单验收
     * @param acceptanceId
     * @return
     */
    @Override
    public OrderWorkAcceptance findOrderWorkAcceptanceByKey(Long acceptanceId, Long orderId) {
        OrderWorkAcceptance orderWorkAcceptance = orderWorkAcceptanceDao.selectByPrimaryKey(acceptanceId, orderId);
        if (Objects.isNull(orderWorkAcceptance)) {
            try (HintManager hintManager = HintManager.getInstance()) {
                hintManager.setMasterRouteOnly();
                return orderWorkAcceptanceDao.selectByPrimaryKey(acceptanceId, orderId);
            }
        }
        return orderWorkAcceptance;
    }

    /**
     * 查询单个订单验收
     * @param acceptanceId
     * @return
     */
    @Override
    public OrderWorkAcceptance findOrderWorkAcceptanceByKeySrcMaster(Long acceptanceId, Long orderId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderWorkAcceptanceDao.selectByPrimaryKey(acceptanceId, orderId);
        }
    }

    @Override
    public OrderWorkAcceptance findOrderWorkAcceptanceByQuerySrcMaster(OrderWorkAcceptanceDbQuery query) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderWorkAcceptanceDao.findOrderWorkAcceptanceByQuery(query);
        }
    }

    /**
     * 根据查询条件查询多个订单验收
     * @param query
     * @return
     */
    @Override
    public List<OrderWorkAcceptance> listOrderWorkAcceptanceByQuery(OrderWorkAcceptanceDbQuery query) {
        return orderWorkAcceptanceDao.listOrderWorkAcceptanceByQuery(query);
    }

    /**
     * 根据查询条件查询多个订单验收（查主库）
     * @param query
     * @return
     */
    @Override
    public List<OrderWorkAcceptance> listOrderWorkAcceptanceByQuerySrcMaster(OrderWorkAcceptanceDbQuery query) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderWorkAcceptanceDao.listOrderWorkAcceptanceByQuery(query);
        }
    }

    /**
     * 修改订单验收
     * @param dto
     */
    @Override
    @Transactional
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_ACCEPTANCE, beforeProceed = false)
    public void updateOrderWorkAcceptanceByKey(OrderWorkAcceptanceDTO dto) {
        orderWorkAcceptanceDao.updateByPrimaryKeySelective(dto.getOrderWorkAcceptance());
    }

    /**
     * 更新所有确认类型验收
     * @param dto
     */
    @Override
    @OrderWorkChangeSync
    public void updateOrderWorkConfirmAcceptance(OrderWorkAcceptanceDTO dto) {
        OrderWorkAcceptance orderWorkAcceptance = BeanMapper.map(dto, OrderWorkAcceptance.class);
        orderWorkAcceptanceDao.updateOrderWorkConfirmAcceptance(orderWorkAcceptance);
    }

    /**
     * 发起验收
     * @param dto
     */
    @Override
    @Transactional
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_ADD_ACCEPTANCE, beforeProceed = false)
    public void insertOrderWorkAcceptance(OrderWorkAcceptanceDTO dto) {
        OrderWorkAcceptance orderWorkAcceptance = BeanMapper.map(dto, OrderWorkAcceptance.class);

        orderWorkAcceptance.setCreater(dto.getOperator());
        orderWorkAcceptance.setUpdater(dto.getOperator());
        orderWorkAcceptance.setCreateTime(dto.getOperateTime());
        orderWorkAcceptance.setUpdateTime(dto.getOperateTime());
        orderWorkAcceptanceDao.insertSelective(orderWorkAcceptance);
        dto.setAcceptanceId(orderWorkAcceptance.getAcceptanceId());
    }

    /**
     * 发起定金确认
     * @param dto
     */
    @Transactional
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_ADD_DEPOSIT_ACCEPTANCE, beforeProceed = false)
    @Override
    public void insertOrderWorkDepositAcceptance(OrderWorkAcceptanceDTO dto) {
        OrderWorkAcceptance orderWorkAcceptance = BeanMapper.map(dto, OrderWorkAcceptance.class);

        orderWorkAcceptance.setCreater(dto.getOperator());
        orderWorkAcceptance.setUpdater(dto.getOperator());
        orderWorkAcceptance.setCreateTime(dto.getOperateTime());
        orderWorkAcceptance.setUpdateTime(dto.getOperateTime());
        orderWorkAcceptanceDao.insertSelective(orderWorkAcceptance);
        dto.setAcceptanceId(orderWorkAcceptance.getAcceptanceId());
    }

    @Override
    @Transactional
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_DEPOSIT_ACCEPTANCE, beforeProceed = false)
    public void depositConfirmAcceptance(OrderWorkAcceptanceDTO dto) {
        orderWorkAcceptanceDao.updateByPrimaryKeySelective(dto.getOrderWorkAcceptance());
    }

    @Override
    @Transactional
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_ENGINEER_DEPOSIT_ACCEPTANCE, beforeProceed = false)
    public void engineerConfirmAcceptance(OrderWorkAcceptanceDTO dto) {
        orderWorkAcceptanceDao.updateByPrimaryKeySelective(dto.getOrderWorkAcceptance());
    }

    @Override
    @Transactional
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_ENGINEER_ACCEPTANCE, beforeProceed = false)
    public void engineerAcceptance(OrderWorkAcceptanceDTO dto) {
        orderWorkAcceptanceDao.updateByPrimaryKeySelective(dto.getOrderWorkAcceptance());
    }

    @Override
    @Transactional
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_CANCEL_DEPOSIT_ACCEPTANCE, beforeProceed = false)
    public void cancelDepositAcceptance(OrderWorkAcceptanceDTO dto) {
        orderWorkAcceptanceDao.updateByPrimaryKeySelective(dto.getOrderWorkAcceptance());
    }

    @Override
    @Transactional
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_CANCEL_ACCEPTANCE, beforeProceed = false)
    public void cancelAcceptance(OrderWorkAcceptanceDTO dto) {
        orderWorkAcceptanceDao.updateByPrimaryKeySelective(dto.getOrderWorkAcceptance());
    }

    @Override
    public void deleteAcceptanceByOrderId(Long orderId) {
        orderWorkAcceptanceDao.deleteOrderWorkAcceptanceByOrderId(orderId);
    }

    @Override
    public void deleteAcceptanceBykey(Long orderId, Long acceptanceId) {
        orderWorkAcceptanceDao.deleteAcceptanceBykey(orderId, acceptanceId);
    }
}
